//
//  QcAddCaseController.m
//  QcMedical1
//
//  Created by jiawen Du on 2016/12/21.
//  Copyright © 2016年 jiawen Du. All rights reserved.
//

#import "QcAddCaseController.h"
#import "QcBaseTypeController.h"
#import "QcCaseCatalogueController.h"
#import "NetWorkManager.h"
#import "QcManageModel.h"
#import "CaseManageController.h"
#import "MBProgressHUD+HM.h"



@interface QcAddCaseController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>


@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@property (atomic, assign) BOOL canceled;

@end

@implementation QcAddCaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];

}

- (instancetype)initWithCaseRecord:(QcManageModel *)qcManageModel{
    
    if (self = [super init]) {
        
        self.modelListM = qcManageModel;
    }
    return self;
}


- (void)setupUI {
    
    self.navigationItem.title = @"添加病情";
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton :)];
    
    [rightButton setTintColor:[UIColor blackColor]];
    
    self.navigationItem.rightBarButtonItem =rightButton;
    
}
#pragma mark
#pragma mark - 提交按钮监听
- (void)clickRightButton:(UIButton *)button {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
    // Set the bar determinate mode to show task progress.
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    hud.label.text = NSLocalizedString(@"提交中...", @"HUD loading title");
    

    
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
        // Do something useful in the background and update the HUD periodically.
        [self doSomeWorkWithProgress];
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hideAnimated:YES];
            
        [MBProgressHUD showSuccessMessage:@"添加成功✔️"];
            
            [self.navigationController popViewControllerAnimated:YES];

        });
    });
    
    
    self.modelListM.caseType = self.typeLabel.text;
    
    self.modelListM.casedesc = self.caseDesc.text;
    
    self.modelListM.caseInfo = self.infoLabel.text;
    
    self.modelListM.imageP = self.imageV.image;
      
    if(self.modelBlock){
        
        self.modelBlock(self.modelListM);
        
    }
 
    
    
}

- (void)doSomeWorkWithProgress {
    self.canceled = NO;
    // This just increases the progress indicator in a loop.
    float progress = 0.0f;
    while (progress < 1.0f) {
        if (self.canceled) break;
        progress += 0.01f;
        dispatch_async(dispatch_get_main_queue(), ^{
            // Instead we could have also passed a reference to the HUD
            // to the HUD to myProgressTask as a method parameter.
            [MBProgressHUD HUDForView:self.navigationController.view].progress = progress;
        });
        usleep(5000);
    }
}


//类型按钮
- (IBAction)caseTypeButton:(id)sender {
    
    QcCaseCatalogueController *qc = [[QcCaseCatalogueController alloc]init];

    self.chang1.hidden = YES;

    [self.navigationController pushViewController:qc animated:YES];
}

//细分按钮
- (IBAction)caseInfo:(id)sender {
    
    QcBaseTypeController *vc = [[QcBaseTypeController alloc]init];
    
    
    if ( self.typeLabel.hidden == NO) {
        
        self.chang2.hidden = YES;
        
        vc.cil_id = self.cil;
        
        [self.navigationController pushViewController:vc animated:YES];
    }else{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择类型" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    
    [alert addAction:action];
        
    [self presentViewController:alert animated:YES completion:nil];}
   
}

- (IBAction)changeImage:(id)sender {
    //实例化picker控制器
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    
    //sourceType打开相册进入的
    pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    //设置代理,需要遵守两个协议
    pickerController.delegate = self;
    
    [self presentViewController:pickerController animated:YES completion:nil];
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    // 获取到选中的图片
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    
    self.imageV.image = [self imageWithImageSimple:image scaledToSize:CGSizeMake(300, 300)];
    
   
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

#define kcaseinfo @"caseinfo"
#define kcasetype @"caseType"
#define kcindex @"index"
-(void)viewWillAppear:(BOOL)animated{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changLabel :) name:kcasetype object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changLabel2 :) name:kcaseinfo object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changcil_id :) name:kcindex object:nil];
    
}

- (void)changcil_id:(NSNotification *)noti {
    
    self.cil = [(noti.object)intValue];

}

- (void)changLabel2 :(NSNotification *)noti{
    
    self.infoLabel.text = noti.object;
    
     self.infoLabel.hidden = NO;
}

- (void)changLabel:(NSNotification *)noti {
    
    self.typeLabel.text = noti.object;
    
    self.typeLabel.hidden = NO;
    
}

- (void)dealloc{
    // 在监听者对象被释放的时候，一定要操作通知中心，注销监听者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark
#pragma mark - 懒加载模型
- (QcManageModel *)modelListM {
    
    if (!_modelListM) {
        _modelListM = [[QcManageModel alloc] init];
    }
    
    return _modelListM;
}

#pragma mark
#pragma mark - 压缩图片
- ( UIImage *)imageWithImageSimple:( UIImage *)image scaledToSize:( CGSize )newSize

{
    
    // Create a graphics image context
    
    UIGraphicsBeginImageContext (newSize);
    
    // Tell the old image to draw in this new context, with the desired
    
    // new size
    
    [image drawInRect : CGRectMake ( 0 , 0 ,newSize. width ,newSize. height )];
    
    // Get the new image from the context
    
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
    
    // End the context
    
    UIGraphicsEndImageContext ();
    
    // Return the new image.
    
    return newImage;
    
}

@end
